package com.log6z.dashborad.utils;

import java.math.BigDecimal;


/**
 * 数字转  大写
 */
public class NumberToCapitalUtil {


    /**
     * 汉语中数字大写
     */
    private static final String[] CN_UPPER_NUMBER = {"零", "壹", "贰", "叁", "肆",
            "伍", "陆", "柒", "捌", "玖"};
    /**
     * 汉语中货币单位大写，这样的设计类似于占位符
     */
    private static final String[] CN_UPPER_MONETRAY_UNIT = {"分", "角", "元",
            "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "兆", "拾",
            "佰", "仟"};

    /**
     * 特殊字符：整
     */
    private static final String CN_FULL = "整";

    /**
     * 特殊字符：负
     */
    private static final String CN_NEGATIVE = "负";

    /**
     * 金额的精度，默认值为2
     */
    private static final int MONEY_PRECISION = 2;

    /**
     * 特殊字符：零元整
     */
    private static final String CN_ZEOR_FULL = "零";


    /**
     * 把数字 转大写
     * @param numberOfMoney
     * @return  对应的汉语大写
     */

    public static String number2CNMontrayUnit(BigDecimal numberOfMoney) {
        StringBuffer sb = new StringBuffer();
        // positive.
        int signum = numberOfMoney.signum();
        // 零元整的情况
        if (signum == 0) {
            return CN_ZEOR_FULL;
        }
        BigDecimal numberStr = numberOfMoney;

        //小数点后
        String suffix = null;

        if (numberOfMoney.toString().contains(".")){
            String numberString = String.valueOf(numberOfMoney.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            String[] number = numberString.split("\\.");
            numberStr = new BigDecimal(number[0]);
            suffix = number[1];
        }


        //这里会进行金额的四舍五入
        long number = numberStr.movePointRight(MONEY_PRECISION)
                .setScale(0, 4).abs().longValue();
        // 得到小数点后两位值
        long scale = number % 100;
        int numUnit = 0;
        int numIndex = 0;
        boolean getZero = false;
        // 判断最后两位数，一共有四中情况：00 = 0, 01 = 1, 10, 11
        if (!(scale > 0)) {
            numIndex = 2;
            number = number / 100;
            getZero = true;
        }
        if ((scale > 0) && (!(scale % 10 > 0))) {
            numIndex = 1;
            number = number / 10;
            getZero = true;
        }
        int zeroSize = 0;
        while (true) {
            if (number <= 0) {
                break;
            }
            // 每次获取到最后一个数
            numUnit = (int) (number % 10);
            if (numUnit > 0) {
                if ((numIndex == 9) && (zeroSize >= 3)) {
                    sb.insert(0, CN_UPPER_MONETRAY_UNIT[6]);
                }
                if ((numIndex == 13) && (zeroSize >= 3)) {
                    sb.insert(0, CN_UPPER_MONETRAY_UNIT[10]);
                }
                sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
                sb.insert(0, CN_UPPER_NUMBER[numUnit]);
                getZero = false;
                zeroSize = 0;
            } else {
                ++zeroSize;
                if (!(getZero)) {
                    sb.insert(0, CN_UPPER_NUMBER[numUnit]);
                }
                if (numIndex == 2) {
                    if (number > 0) {
                        sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
                    }
                } else if (((numIndex - 2) % 4 == 0) && (number % 1000 > 0)) {
                    sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
                }
                getZero = true;
            }
            // 让number每次都去掉最后一个数
            number = number / 10;
            ++numIndex;
        }
        // 如果signum == -1，则说明输入的数字为负数，就在最前面追加特殊字符：负
        if (signum == -1) {
            sb.insert(0, CN_NEGATIVE);
        }
        StringBuilder str = new StringBuilder();
        if (suffix != null){
            str.append("点");
            for (char c : suffix.toString().toCharArray()) {
                str.append(CN_UPPER_NUMBER[Integer.valueOf(String.valueOf(c))]);
            }
        }
        String s = sb.toString();
        s = s.replace("元", "");
        s = s.replace("分","");
        s = s.replace("角","");
        return s+str.toString();
    }

    public static void main(String[] args) {
        System.out.println(number2CNMontrayUnit(new BigDecimal(127)));
        System.out.println(number2CNMontrayUnit(new BigDecimal(800)));
        System.out.println(number2CNMontrayUnit(new BigDecimal(800.12)));
        System.out.println(number2CNMontrayUnit(new BigDecimal(850.00)));
        System.out.println(number2CNMontrayUnit(new BigDecimal(801.01)));
        System.out.println(number2CNMontrayUnit(new BigDecimal(152.35)));
        System.out.println(number2CNMontrayUnit(new BigDecimal(152.10)));
        System.out.println(number2CNMontrayUnit(new BigDecimal(0)));
        System.out.println(number2CNMontrayUnit(new BigDecimal(0.00)));
        System.out.println(number2CNMontrayUnit(new BigDecimal(0.0)));

    }


}

